package com.speedricemanager.other.server;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class DataFormate {
    public static final String StringF="yyyy-MM-dd HH:mm:ss";
    public static final String StringM="yyyy-MM-dd HH:mm";
    public static final String StringD="MM月dd日 HH:mm";
    public static final String StringT = "HH:mm";
    public static final String Stringd = "dd";
    public static final String Stringmm = "mm";
    public static final String StringMM = "MM";
    public static final String StringHH = "HH";
    public static final String Stringyyyy = "yyyy";

    public static String formate(String mat, Object obj){//"yyyy年MM月dd日   HH:mm:ss
        SimpleDateFormat formatter   =   new SimpleDateFormat(mat, Locale.CHINA);
        return   formatter.format(obj);
    }
    public static String formate(String mat, Date obj){//"yyyy年MM月dd日   HH:mm:ss
        SimpleDateFormat formatter   =   new SimpleDateFormat(mat, Locale.CHINA);
        return   formatter.format(obj);
    }

    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd HH:mm:ss
     *
     * @param millisecond
     * @return
     */
    public static String getDateTimeFromMillisecond(String mat, Long millisecond){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(mat);
        Date date = new Date(millisecond);
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }

    /**
     * date类型转换为long类型
     *
     * @param date
     * @return
     */

    // date:要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * string类型转换为date类型
     * @param strTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    /**
     * String类型转换为long类型
     * @param strTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    /**
     * 时间显示格式类型
     * @param receiveMsgDateTime 收发信息时间
     *
     * @return
     */
    public static String getShowTime(String receiveMsgDateTime){
        int receiveMsgTimeyyyy = 0;
        int currentTimeyyyy = 0;
        int receiveMsgTimeMM = 0;
        int currentTimeMM = 0;
        int receiveMsgTimed = 0;
        int currentTimed = 0;
        int receiveMsgTimeHH = 0;
        int currentTimeHH = 0;
        int receiveMsgTimemm = 0;
        int currentTimemm = 0;

        String StTime = null;
        try {
            currentTimeyyyy = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringyyyy, System.currentTimeMillis()));
            receiveMsgTimeyyyy = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringyyyy,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            currentTimeMM = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringMM, System.currentTimeMillis()));
            receiveMsgTimeMM = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringMM,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            currentTimed = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringd, System.currentTimeMillis()));
            receiveMsgTimed = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringd,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            currentTimeHH = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringHH, System.currentTimeMillis()));
            receiveMsgTimeHH = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringHH,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            currentTimemm = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringmm, System.currentTimeMillis()));
            receiveMsgTimemm = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringmm,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (currentTimeyyyy == receiveMsgTimeyyyy){
            if (currentTimeMM == receiveMsgTimeMM){
                if (currentTimed - receiveMsgTimed == 0){
                    try {
                        StTime = getDateTimeFromMillisecond(DataFormate.StringT,stringToLong(receiveMsgDateTime,DataFormate.StringF));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }else if(currentTimed - receiveMsgTimed ==1){
                    try {
                        StTime = "昨天 "+getDateTimeFromMillisecond(DataFormate.StringT,stringToLong(receiveMsgDateTime,DataFormate.StringF));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }else if(currentTimed - receiveMsgTimed == 2){
                    try {
                        StTime = "前天 "+getDateTimeFromMillisecond(DataFormate.StringT,stringToLong(receiveMsgDateTime,DataFormate.StringF));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }else {
                    try {
                        StTime = getDateTimeFromMillisecond(DataFormate.StringD,stringToLong(receiveMsgDateTime,DataFormate.StringF));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }else {
                try {
                    StTime = getDateTimeFromMillisecond(DataFormate.StringD,stringToLong(receiveMsgDateTime,DataFormate.StringF));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }else {
            try {
                StTime = getDateTimeFromMillisecond(DataFormate.StringF,stringToLong(receiveMsgDateTime,DataFormate.StringF));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return StTime;
    }

    public static boolean getTFTime(String receiveMsgDateTime, String upreceiveMsgDateTime){
        int receiveMsgTimeyyyy = 0;
        int upreceiveMsgTimeyyyy = 0;
        int receiveMsgTimeMM = 0;
        int upreceiveMsgTimeMM = 0;
        int receiveMsgTimed = 0;
        int upreceiveMsgTimed = 0;
        int receiveMsgTimeHH = 0;
        int upreceiveMsgTimeHH = 0;
        int receiveMsgTimemm = 0;
        int upreceiveMsgTimemm = 0;

        boolean TFtime ;
        try {
            upreceiveMsgTimeyyyy = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringyyyy, System.currentTimeMillis()));
            receiveMsgTimeyyyy = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringyyyy,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            upreceiveMsgTimeMM = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringMM, System.currentTimeMillis()));
            receiveMsgTimeMM = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringMM,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            upreceiveMsgTimed = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringd, System.currentTimeMillis()));
            receiveMsgTimed = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringd,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            upreceiveMsgTimeHH = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringHH, System.currentTimeMillis()));
            receiveMsgTimeHH = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.StringHH,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
            upreceiveMsgTimemm = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringmm, System.currentTimeMillis()));
            receiveMsgTimemm = Integer.parseInt(getDateTimeFromMillisecond(DataFormate.Stringmm,stringToLong(receiveMsgDateTime,DataFormate.StringF)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (receiveMsgTimeyyyy == upreceiveMsgTimeyyyy){
            if (receiveMsgTimeMM == upreceiveMsgTimeMM){
                if (receiveMsgTimed == upreceiveMsgTimed){
                    if (receiveMsgTimeHH == upreceiveMsgTimeHH){
                        if (receiveMsgTimemm == upreceiveMsgTimemm){
                            TFtime = false;
                        }else {
                            TFtime = true;
                        }
                    }else {
                        TFtime = true;
                    }
                }else {
                    TFtime = true;
                }
            }else {
                TFtime = true;
            }
        }else {
            TFtime = true;
        }

        return TFtime;
    }


    /**
     * 获取时间间隔
     *
     * @param millisecond
     * @return
     */
    public static String getSpaceTime(Long millisecond) {
        Calendar calendar = Calendar.getInstance();
        Long currentMillisecond = calendar.getTimeInMillis();

        //间隔秒
        Long spaceSecond = (currentMillisecond - millisecond) / 1000;

        //一分钟之内
        if (spaceSecond >= 0 && spaceSecond < 60) {
            return getDateTimeFromMillisecond(DataFormate.StringT,millisecond);
        }
        //一小时之内
//        else if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) {
//            return spaceSecond / 60 + "分钟之前";
//        }
        //一天之内
        else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) {
            return spaceSecond / (60 * 60) + "小时之前";
        }
        //3天之内
        else if (spaceSecond/(60*60*24)>0&&spaceSecond/(60*60*24)<3){
            return spaceSecond/(60*60*24)+"天之前";
        }else {
            return getDateTimeFromMillisecond(DataFormate.StringT,millisecond);
        }
    }

}
